import { http } from "../../utils/http"
import { clearToken, setToken } from "../../utils/token"
export const getCode = (mobile) => async () => {
  http.get(`/sms/codes/${mobile}`)
}

export const login = (data) => async (dispatch) => {
  const res = await http.post("/authorizations", data)
  // console.log(res.data.message)
  const { data: token } = res.data
  // 将 token 数据存储到本地缓存中
  setToken(token)
  // 分发 action 将 token 存储到 redux 中
  dispatch({ type: "SET_TOKEN", payload: token })
}

// 退出
export const logout = () => {
  return (dispatch) => {
    // 清空本地缓存中 token
    clearToken()
    // 清空 redux 中存储的 token
    dispatch({ type: "CLEAR_TOKEN" })
  }
}
